//
//  SelectedChatEntryView.m
//  FKGW
//
//  Created by song ce on 2017/6/3.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "SelectedChatEntryView.h"
#import "UIView+Extend.h"

@interface SelectedChatEntryView ()
@property(nonatomic,strong)UIButton *recordBtn;//记录刚刚添加的btn
@property (weak, nonatomic) IBOutlet UILabel *noEmployeeOnlineView;

@end
@implementation SelectedChatEntryView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.width = 300;
    self.noEmployeeOnlineView.hidden = YES;
}

-(void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    if(!_dataArray.count){
        self.noEmployeeOnlineView.hidden = NO;
    }else{
        self.noEmployeeOnlineView.hidden = YES;
    }
    [self initwithSubViews];
}

-(void)initwithSubViews{
    
    [self initWaterButton];
}

//流水布局按钮
- (void)initWaterButton{
//    UIImage *newImage = [self resizableImage:@"xinxikuang"];
    //    UIImage *newImage = [UIImage imageNamed:@"xinxikuang"];
    CGFloat spaceWith = 10;
    for (int i = 0; i < _dataArray.count; i ++)
    {
        ChatEntryModel *model = _dataArray[i];
        NSString *name = model.nickName;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:11];
        [btn setTitleColor:DEF_COLOR153 forState:UIControlStateNormal];
        CGRect rect = [name boundingRectWithSize:CGSizeMake(1000 , 15) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:btn.titleLabel.font} context:nil];
        CGFloat BtnW = rect.size.width + 20;
        CGFloat BtnH = 20;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = BtnH/2;
        if (i == 0)
        {
                btn.frame =CGRectMake(15, 55, BtnW, BtnH);
        }
        else{
            CGFloat yuWidth = 300  -self.recordBtn.frame.origin.x-self.recordBtn.frame.size.width;
            if (yuWidth >= BtnW+15+spaceWith) {
                btn.frame =CGRectMake(self.recordBtn.frame.origin.x+self.recordBtn.frame.size.width+ spaceWith, self.recordBtn.top, BtnW, BtnH);
            }else{
                btn.frame =CGRectMake(15, self.recordBtn.bottom+10, BtnW, BtnH);
            }
        }
        btn.tag = 100+i;
        btn.backgroundColor = [UIColor clearColor];
        [btn setTitle:name forState:UIControlStateNormal];
        [self addSubview:btn];
        self.recordBtn = btn;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setBackgroundColor:DEF_COLOR242];
//        [btn setBackgroundImage:newImage forState:UIControlStateNormal];
   }
}

-(void)btnClick:(UIButton*)btn{
    ChatEntryModel *model = self.dataArray[btn.tag-100];
    NSString *userName =model.userName;
    if(self.creatChatBlock){
        self.creatChatBlock(userName,model.nickName);
    }
}


- (IBAction)closeAction:(UIButton *)sender {
    if(self.closeBlock){
        self.closeBlock();
    }
}

//拉伸按钮的背景图片
-(UIImage *)resizableImage:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    CGFloat h = image.size.width * 0.5;
    CGFloat w = image.size.height * 0.5;
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(w, h, w, h) resizingMode:UIImageResizingModeStretch];
}



@end
